//
//  WelcomeViewController.swift
//  Microblog
//
//  Created by steaest on 15/8/2.
//  Copyright © 2015年 steaest. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    //记录底部约束,用于动画
    private var iconViewBottom: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        if let urlString = UserAccount.sharedAccount?.avatar_large{
        
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewBottom?.constant = -UIScreen.mainScreen().bounds.height - iconViewBottom!.constant
        
       UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
        self.view.layoutIfNeeded()
        }) { (_) -> Void in
            
            NSNotificationCenter.defaultCenter().postNotificationName(WWRootViewControllerSwitchNotification, object: true)
            
        }
    }

    private func prepareUI() {
    
        //1.加载控件
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        
        //2.设置自动布局
        //背景
        backImageView.ff_Fill(view)
        //头像
        let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSizeMake(90, 90), offset: CGPointMake(0, -160))
        iconViewBottom = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        //labei
        label.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPointMake(0, 16))
    }
    
    //MARK:懒加载控件
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //切圆角
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        
        return iv
    }()
    
    private lazy var label: UILabel = {
        
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.sizeToFit()
        
        return label
    }()
}
